package com.example.wxy.bbtapp;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.Toast;


import com.example.wxy.bbtapp.Startup.DbService;
import com.example.wxy.bbtapp.Models.PhoneInfo;
import com.example.wxy.bbtapp.Models.bindinfo;

import java.util.Date;

/**
 * Created by wxy on 2017/9/6.
 */

public class myApplication extends Application {

    public static String Token = null;
    public static Date TokenTime = new Date();

    public static bindinfo bindinofs = new bindinfo();

    public static PhoneInfo phoneInfo = new PhoneInfo();

    public  static String AppVer = "0.9";

    private static Context context;

    //返回
    public static Context getContext(){
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        // 开启服务
        //startService(new Intent(this, BBTService.class));
        // 配置数据库
        DbService. DBSetting(this);
        // 获取设置信息
        getDeviceInfo();
    }



    private void getDeviceInfo() {
        //获取mac地址
        WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        phoneInfo.setPhoneVersion(android.os.Build.VERSION.RELEASE);
        phoneInfo.setMacAddress(info.getMacAddress());
        phoneInfo.setPhoneName(android.os.Build.MODEL);

        //获取IP地址
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        //判断wifi是否链接
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (!mWifi.isConnected()) {
            Toast.makeText(this, "请连接wifi来进行操作", Toast.LENGTH_SHORT);
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        String ip = intToIp(ipAddress);
        phoneInfo.setIpAdress(ip);
    }

    private String intToIp(int i) {

        return (i & 0xFF ) + "." +
                ((i >> 8 ) & 0xFF) + "." +
                ((i >> 16 ) & 0xFF) + "." +
                ( i >> 24 & 0xFF) ;
    }










}
